$(function () {
    var $datagrid = $("#datagrid"), $toolbar = $("#toolbar"), $dialog=$("#dialog");
    $("#saveCoupon").on("click",function(){
        openDialog();
    });
    $("#editCoupon").on("click",function(){
        var rows = $datagrid.datagrid("getSelected");
        if(rows){
            var url = "edit.jsp?couponId="+rows.Id+"&amount="+rows.TotalNumber+"&startTime="+rows.StartTime+"&endTime="+rows.EndTime;
            openDialog(url);
        }else{
            Constant.showMessage("请选择目标！");
        }
    });
    $datagrid.datagrid({
        singleSelect: true,
        fit: true,
        border: false,
        fitColumns: true,
        url: Constant.ctx("/mulanweb/admin/coupon/search"),
        method: 'post',
        pagination: true,
        toolbar: $toolbar,
        columns: [[
            {field: 'Id', title: '编号'},
            {field: 'CouponCode', title: '券码'},
            {field: 'SatisfyValue', title: '条件'},
            {field: 'Value', title: '面额'},
            {field: 'TotalNumber', title: '数量'},
            {field:'SurplusNumber',title:'剩余数量'},
            {field:'CreateTime',title:'创建时间'},
            {field:'StartTime',title:'开始时间'},
            {field:'EndTime',title:'结束时间'},
            {field: 'AdminName', title: '操作者'},
            {field: 'ExtensionName', title: '推广者'},
            {field:'StatusId',title:'状态',formatter:function(v,r,i){
                console.info(v);
                if(v == 10 ){
                    return "有效"
                }else if(v ==12){
                    return "失效";
                }else if(v ==11){
                    return "已售完";
                }
            }},
            {field:'DeleteTime',title:'操作',formatter:function(v,r,i){
                return '<a href="#" onclick="deleteCoupon(\''+r.Id+'\')">删除</a>';
            }},
        ]],
       onBeforeLoad: function (param) {
            param.PageNumber = param.page;
            param.PageSize = param.rows;
            return true;
        },
        loadFilter: function (data) {
            var newData = {};
            if (data.ReturnCode == 1) {
                newData.rows = data.DataList;
                if (newData.rows.length > 0) {
                    newData.total = data.TotalRow;
                } else {
                    newData.total = 0;
                }
            }
            return newData;
        }
    });
    function saveCoupon() {
        $("form").form("submit", {
            url: $("form").attr("action"),
            onSubmit: function () {
                var isValid = $(this).form('validate');
                if (!isValid) {
                    $.messager.progress('close');	// hide progress bar while the form is invalid
                }
                return isValid;
            },
            success: function (respText) {
                var  resp= $.parseJSON(respText);
                console.info(respText);
                console.info("respText.ReturnCode="+respText.ReturnCode);
                console.info("resp="+resp.ReturnCode);
                Constant.showMessage(resp.AlertMessage,resp.message, "操作成功");
                if (resp.ReturnCode == 1) {
                    $("#datagrid").datagrid("reload");
                    $dialog.dialog("close");
                }
            }
        });
    }
    function openDialog(url) {
        $dialog.dialog({
            title: url?"编辑":"新增优惠券",
            width: 400,
            height: 300,
            closed: false,
            cache: false,
            href: url?url:'form.jsp',
            modal: true,
            onClose: function () {
                $("#form-dialog").dialog("destroy");
            },
            buttons: [{
                text: url?'保存':'确定生成',
                handler: saveCoupon
            }]
        });
    }
})

function deleteCoupon(couponId){
    $.messager.confirm('警告', '您确定删除此优惠券吗？', function (r) {
        $.post(Constant.ctx("/mulanweb/admin/coupon/delete"),{couponId:couponId},function(info){
            if (info.ReturnCode == 1) {
                $("#datagrid").datagrid("reload");
                Constant.showMessage(info.AlertMessage,info.message,"操作成功");
            }else{
                Constant.showMessage(info.AlertMessage,info.message);
            }
        })
    })
}